<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

	function getNextGame($ploegid){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		$wedstmt = "select wed.wedid from weds wed where wed.datum >=" . current_date . " and wed.ploid=" . $ploegid . " order by wed.datum rows 1";
		$wedquery = ibase_query($conn, $wedstmt);
		while ($row = ibase_fetch_object($wedquery)){
				$wedid=$row->WEDID;
		}
		ibase_free_result($wedquery);     
		ibase_close($conn);
		   return $wedid;
	}

       

	function getWedstrijdDates($ploegid){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		$dateStmt = "select wed.datum, wed.wedid, wed.ploid,wed.tegen from weds wed ";
		$dateStmt .="where wed.ploid in (" . $ploegid . ")";
		$dateStmt .=" and wed.datum > current_date - 1 ";
		$dateStmt .="and wed.datum < current_date +60 ";
		$dateStmt .="order by datum asc;";
		$dateQuery = ibase_query($conn, $dateStmt);
		while ($row = ibase_fetch_object($dateQuery)){
		   $label = $row->DATUM . " (" . $row->TEGEN . ")";
		   $arr[] = array ('wedid'=>$row->WEDID,'datum'=>$label);
		}
		ibase_free_result($dateQuery);
		ibase_close($conn);
		return $arr;
	}

	function getWedstrijdPlayers($wedid,$absent){
	   $conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'gast', 'rcl'); 
	   $stmt = "select lid.rnaam,lid.vnaam,lid.naam, lid.lidid from wede wed ";
	   $stmt .="join faml lid on wed.lidid=lid.lidid ";
	   $stmt .="where wed.wedid='" . $wedid . "'";
	   $stmt .=" and swafw = '" . $absent . "' ";
	   $stmt .=" order by lid.vnaam";
	   $query = ibase_query($conn, $stmt);
	   while ($row = ibase_fetch_object($query)){
		   $name = $row->VNAAM . " " . $row->NAAM;
		   $arr[] = array ('lidid'=>$row->LIDID,'name'=>$name);
	   }
	   ibase_free_result($query);
	   ibase_close($conn);
	   return $arr;
	}

	function getPresentPlayers($datum,$prono,$catid){
	   $conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'gast', 'rcl'); 
	   $stmt = "select trtype.weekd, tr.datum,tr.aanwe, lid.rnaam,lid.vnaam,lid.naam, lid.lidid from trad tr ";
	   $stmt .="join faml lid on tr.lidid=lid.lidid ";
	   $stmt .="join trma trtype on tr.trmid=trtype.trmid ";
	   $stmt .="join trmc cat on (trtype.trmid=cat.trmid and cat.catid=$catid) ";
	   $stmt .="where tr.datum='" . $datum . "'";
	   $stmt .=" and (tr.aanwe = 'T' or tr.aanwe is null)";
	   $stmt .=" and tr.prono ='" . $prono . "'";
	   $stmt .=" order by lid.vnaam";
	   $query = ibase_query($conn, $stmt);
	   $arr=array();
          while ($row = ibase_fetch_object($query)){
		   $name = $row->VNAAM . " " . $row->NAAM;
		   $confirmed=false;
		   if ($row->AANWE=="T"){
			   $confirmed=true;
		   }
		   $arr[] = array ('lidid'=>$row->LIDID,'name'=>$name,'confirmed'=>$confirmed);
	   }
          ibase_free_result($query);
	   ibase_close($conn);
	   return $arr;
	}


	function getSpelerStatus($lidid){
	   $conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
	   $stmt = "select Lidid, Naam, Email, GSM, Med_Status, Med_Seizoen, VRB_Licentie, VRB_Type, VRB_Type_T, VRB_Status, Lidgeld_Status, Lidgeld_Seizoen,";
	   $stmt .= "Adres, Postcode, Woonplaats, Geboorte_datum, speler,vord_ogm, vord_lg1_text,vord_lg1, vord_lg2_text,vord_lg2, vord_lg3_text,vord_lg3, vord_lg4_text,vord_lg4 From LIJST_LIDID_INFO_SITE(" . $lidid . ")";
	   $query = ibase_query($conn, $stmt);
	   while ($row = ibase_fetch_object($query)){
                 $arr['id']=$row->LIDID;
		   $arr['naam']=$row->VNAAM . " " . $row->NAAM;
		   $arr['email']=$row->EMAIL;
		   $arr['gsm']=$row->GSM;
		   $arr['medStatus']=$row->MED_STATUS;
		   $arr['medSeizoen']=$row->MED_SEIZOEN;
		   $arr['vrbLic']=$row->VRB_LICENTIE;
	          $arr['vrbType']=$row->VRB_TYPE;
		   $arr['vrbStatus']=$row->VRB_STATUS;
		   $arr['lidgeldStatus']=$row->LIDGELD_STATUS;
		   $arr['lidgeldSeizoen']=$row->LIDGELD_SEIZOEN;
		   $arr['adres']=$row->ADRES;
		   $arr['postcode']=$row->POSTCODE;
		   $arr['woonplaats']=$row->WOONPLAATS;
		   $arr['geboorteDatum']=$row->GEBOORTE_DATUM;
		   $arr['isSpeler']=$row->SPELER;
                 $arr['ogm']=$row->VORD_OGM;
		   $arr['payOption1']=$row->VORD_LG1_TEXT;
                 $arr['payAmount1']=$row->VORD_LG1;
                 $arr['payOption2']=$row->VORD_LG2_TEXT;
                 $arr['payAmount2']=$row->VORD_LG2;
                 $arr['payOption3']=$row->VORD_LG3_TEXT;
                 $arr['payAmount3']=$row->VORD_LG3;
                 $arr['payOption4']=$row->VORD_LG4_TEXT;
                 $arr['payAmount4']=$row->VORD_LG4;


	   }
	   ibase_free_result($query);
	   ibase_close($conn);
	   return $arr;
	}

	function getTrainingDates($cat){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'gast', 'rcl'); 
		$dateStmt = "select tr.trmid,tr.datum from trai tr,trmc tc ";
		$dateStmt .= "where tr.trmid=tc.trmid ";
		$dateStmt .= "and tr.datum > current_date - 20 ";
		$dateStmt .= "and tc.catid=" . $cat;
		$dateStmt .= " order by tr.datum asc ";
		$dateQuery = ibase_query($conn, $dateStmt);
		while ($row = ibase_fetch_object($dateQuery)){
		   $arr[] = array ('trmid'=>$row->TRMID,'datum'=>$row->DATUM);
		}
		ibase_free_result($dateQuery);
		ibase_close($conn);
		return $arr;
	}



	function getAllPlayers(){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		$stmt="select lijst.lidid, lid.rnaam, lid.vnaam, lid.naam from LIJST_LEDEN_HUIDIG_LEEFTIJDSCAT lijst ";
		$stmt .="join faml lid on lijst.lidid=lid.lidid ";
		$stmt .="where lijst.CATID in (3,4,5)";
		$stmt .= " order by lid.rnaam";
		$query = ibase_query($conn, $stmt);
		$arr[] = array ('lidid'=>0,'naam'=>'Selecteer');
		while ($spelerrow = ibase_fetch_object($query)){
			$name = $spelerrow->VNAAM . " " . $spelerrow->NAAM;
			$arr[] = array ('lidid'=>$spelerrow->LIDID,'naam'=>$name);
		}
		ibase_free_result($query);
		ibase_close($conn);
		return $arr;
	}


	function getOpstelling($wedid){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		$stmt = "select wed.tegen,wed.wedid,lid.lidid, lid.rnaam, aanw.posit, aanw.swbev from weds wed ";
		$stmt .=" join wede aanw on wed.wedid=aanw.wedid ";
		$stmt .=" join faml lid on aanw.lidid = lid.lidid ";
		$stmt .=" where wed.wedid=" . $wedid . " and aanw.posit !=0";
		$stmt .=" order by aanw.posit ";
		$query = ibase_query($conn, $stmt);
		while ($row = ibase_fetch_object($query)){
			$arr[] = array ('lidid'=>$row->LIDID,'naam'=>$row->RNAAM,'position'=>$row->POSIT);
		}
		ibase_free_result($query);
		ibase_close($conn);
		return $arr;
	}


	function getAanwWedstrijdDates($lidid,$cat){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		$stmt ="select wed.datum, wed.wedid, wed.tegen, wed.platy, wed.ploid, wed.wedty, aanw.lidid, aanw.swafw, aanw.posit from weds wed ";
		$stmt .="join ploe ploeg on (wed.ploid=ploeg.ploid and ploeg.catid =" . $cat . ") ";
		$stmt .="left join wede aanw on (aanw.wedid=wed.wedid and aanw.lidid=$lidid) ";
		$stmt .="where wed.datum > CURRENT_DATE - 1 ";
		$stmt .="and wed.datum < CURRENT_DATE + 50 ";
		$stmt .="order by wed.datum asc";
		$query = ibase_query($conn, $stmt);
		while ($row = ibase_fetch_object($query)){
		if ($row->PLOID == 7){
			  $rclploeg='Leuven 2';
		   } else {
			  $rclploeg='Leuven';
		}
		if ($row->PLATY == 'T'){
			   $ploeg1=$rclploeg;
			   $ploeg2=$row->TEGEN;
		   } else {
			   $ploeg1=$row->TEGEN;
			  $ploeg2=$rclploeg;
		   }
		   $aanwezig=0;
		   if ($row->SWAFW == 'F'){
			   $aanwezig=1;
		   } else if ($row->SWAFW == 'T'){
			   $aanwezig=-1;
		   }  
		   $arr[] = array ('value'=>$row->WEDID,'label'=>$row->DATUM,'ploeg1'=>$ploeg1,'ploeg2'=>$ploeg2,'aanwezig'=>$aanwezig);
		}
		ibase_free_result($query);
		ibase_close($conn);
		return $arr;
	}

	function getGameResults(){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'gast', 'rcl'); 
		$stmt="select wed.wedid, wed.platy, wed.tegen, wed.ploid, wed.uitsl, wed.wsurl, wed.urlvi, plo.shnam from weds wed ";
		$stmt .="  left join Ploe plo on (wed.PloId = plo.PloId) ";
		$stmt .="where wed.datum < current_date ";
		$stmt .="and wed.ploid = 4";
		$stmt .=" and seiid = 2011";
		$stmt .=" order by wed.datum, wed.uur"; 
		$query = ibase_query($conn, $stmt);
		while ($row = ibase_fetch_object($query)){
		if ($row->PLATY == 'T'){
			   $ploeg1='Leuven';
			   $ploeg2=$row->TEGEN;
		   } else {
			  $ploeg1=$row->TEGEN;
			  $ploeg2='Leuven';
		   }
		   $arr[] = array ('ploeg1'=>$ploeg1,'ploeg2'=>$ploeg2,'result'=>$row->UITSL,'report'=>$row->WSURL, 'video'=>$row->URLVI);
		}
		ibase_close($conn);
		return $arr;
	}

       function getTrainersDates($lidid,$soort){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		$stmt = "select trainday.lidid, trainday.datum, trainday.soort, trainday.trmid,trainday.wedid,trainday.aanwe ";
              $stmt .="from LIJST_TRAINERSDAGEN_AANWE trainday ";
		$stmt .="left join TrMa m on (m.TrMId = trainday.TrMId) ";
		$stmt .=" where trainday.lidid = $lidid";
		$stmt .=" and trainday.soort = '" . $soort . "'";
		$stmt .=" order by trainday.datum";
		$query = ibase_query($conn, $stmt);
		while ($row = ibase_fetch_object($query)){
		   $aanwezig=0;
                 if ($row->AANWE == 'T'){
			$aanwezig=1;
		   } else if ($row->AANWE == 'F'){
			$aanwezig=-1;
		   }  
		   $arr[] = array ('value'=>$row->TRMID,'wedid'=>$row->WEDID,'label'=>$row->DATUM,'aanwezig'=>$aanwezig);
		}
		ibase_free_result($query);
		ibase_close($conn);
		return $arr;
       }


	function confirmTrainerTrainingPresence($trmid,$lidid,$date,$aanwezig){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		$stmt = "execute PROCEDURE TRAINING_EFFECTIEF_AANWEZIG ($trmid,$lidid,'" . $aanwezig . "','" . $date . "','T')";
		$query = ibase_query($conn, $stmt);
              ibase_close($conn);
              return "{'success':true}";
	}

	function confirmTrainerGamePresence($wedid,$lidid,$aanwezig){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		$stmt = "execute PROCEDURE WEDSTRIJD_BEVESTIG_AANWEZIGHEID($wedid,$lidid,'" . $aanwezig . "','T')";
		$query = ibase_query($conn, $stmt);
              ibase_close($conn);
              return "{'success':true}";
	}

	function getAanwTrainingDates($lidid,$cat){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		$stmt = "select trtype.weekd,tr.datum, tr.trmid,tra.lidid,tra.prono,tra.swtra from trai tr ";
		$stmt .="join trma trtype on tr.trmid=trtype.trmid ";
		$stmt .="join trmc tc on (tr.trmid=tc.trmid and tc.catid=" . $cat . ") ";
		$stmt .="left join trad tra on (tr.trmid=tra.trmid and tr.datum=tra.datum and tra.lidid=$lidid) ";
		$stmt .="where tr.datum > current_date ";
		$stmt .="and tr.datum < current_date + 45 ";
		$stmt .="order by tr.datum asc";

		$query = ibase_query($conn, $stmt);
		while ($row = ibase_fetch_object($query)){
		   $aanwezig=0;
                 if ($row->PRONO == 'T'){
			$aanwezig=1;
		   } else if ($row->PRONO == 'F'){
			$aanwezig=-1;
		   }  
		   $arr[] = array ('value'=>$row->TRMID,'label'=>$row->DATUM,'aanwezig'=>$aanwezig,'weekdag'=>$row->WEEKD);
		}
		ibase_free_result($query);
		ibase_close($conn);
		return $arr;
	}

	function confirmWedPresence($wedid,$lidid,$insertorupdate){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		if ($insertorupdate=='update'){
			$stmt = "update wede set swafw = 'F' where lidid=" . $lidid . " and wedid = " . $wedid ;
		} else {
			$stmt = "insert into wede(wedid,lidid,posit,swafw,swtra,swbeg,swspe) values (" . $wedid . "," . $lidid . ",0,'F','F','F','T')";
		}
		$query = ibase_query($conn, $stmt);
		ibase_close($conn);
		return "{'success':true}";
	}

	function confirmWedAbsence($wedid,$lidid,$insertorupdate){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		if ($insertorupdate=='update'){
			$stmt = "update wede set swafw = 'T' where lidid=" . $lidid . " and wedid = " . $wedid ;
		} else {
			$stmt = "insert into wede(wedid,lidid,posit,swafw,swtra,swbeg,swspe) values (" . $wedid . "," . $lidid . ",0,'T','F','F','T')";
		}
		$query = ibase_query($conn, $stmt);
		ibase_close($conn);
		return "{'success':true}";
	}

	function confirmTrainingPresencePlayers($trmid,$playerIds,$date){
		$result=" statement : ";
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		for($i = 0; $i < sizeof($playerIds); ++$i){
			$stmt = "update trad set aanwe = 'T' where trmid = " . $trmid ;
			   $stmt .= " and datum ='". $date . "' and lidid = " . $playerIds[$i]; 
			$query = ibase_query($conn, $stmt);
		}
		ibase_close($conn);
		return "{'success':true}";
	}

	function confirmTrainingPresence($trmid,$lidid,$date,$insertorupdate){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		if ($insertorupdate=='update'){
			$stmt = "update trad set prono='T' where lidid=" . $lidid . " and datum='" . $date . "'"; 
		} else {
			$stmt = "insert into trad(trmid,datum,lidid,prono) values (" . $trmid . ",'" . $date . "'," . $lidid . ",'T')"; 
		}
		$query = ibase_query($conn, $stmt);
		ibase_close($conn);
		return "{'success':true}";
	}

	function confirmTrainingAbsence($trmid,$lidid,$date,$insertorupdate){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		if ($insertorupdate=='update'){
			$stmt = "update trad set prono='F' where lidid=" . $lidid . " and datum='" . $date . "'"; 
		} else {
			$stmt = "insert into trad(trmid,datum,lidid,prono) values (" . $trmid . ",'" . $date . "'," . $lidid . ",'F')"; 
		}
		$query = ibase_query($conn, $stmt);
		ibase_close($conn);
		return "{'success':true}";
	}

	function updatePosition($position,$lidid,$wedid){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl');
		//set postion of previous player to 0
		$stmt = "update wede aanw set posit =0 where wedid=" . $wedid . " and posit=" . $position;
		$query = ibase_query($conn, $stmt);
		if ($lidid!=0){
		//check if player is already in the list
		$exist_stmt = "select 1 as aanwezig from wede aanw where aanw.lidid=" . $lidid . " and wedid=" . $wedid;
		$existquery = ibase_query($conn, $exist_stmt);
		
		if ($row = ibase_fetch_object($existquery)){
			$updatestmt = "update wede aanw set posit=" . $position . " where aanw.lidid=" . $lidid . " and wedid=" . $wedid;
		} else {
			$updatestmt = "insert into wede(wedid,lidid,posit,swafw,swbeg,swtra,swspe) values (" . $wedid . "," . $lidid . "," . $position . ",'F','F','F','T')";
		}
		ibase_query($conn,$updatestmt);
		ibase_free_result($existquery);   
		}
		ibase_close($conn);
		return "{'success':true}";
	}

       function getLidgeldDatum($lidid){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		$stmt="select daten from lidg where lidid=" . $lidid . " order by seiid";
              $query = ibase_query($conn, $stmt);
	       while ($row = ibase_fetch_object($query)){
		   $date =$row->DATEN;
	       }
	       ibase_free_result($query);
              ibase_close($conn);

		$todays_date= date("Y-m-d");
              $today = strtotime($todays_date);
              $expiration_date = strtotime($date);
              if ($expiration_date < $today){
                   return 'Uw licentie is niet meer geldig';
                   
              }
	}


       function getNumberOfTrainingsNotEntered($lidid){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		$stmt="select count(tr.datum) from trai  tr";
              $stmt.=" join trmc tc on (tr.trmid=tc.trmid and tc.catid=4)";
              $stmt.=" left join trad tra on (tr.trmid=tra.trmid and tr.datum=tra.datum and tra.lidid=" . $lidid . ")";
              $stmt.=" where tr.datum > current_date";
              $stmt.=" and tr.datum < current_date + 45";
              $stmt.=" and tra.prono is null";
              $query = ibase_query($conn, $stmt);
	       while ($row = ibase_fetch_object($query)){
		   $number =$row->COUNT;
	       }
	       ibase_free_result($query);
              ibase_close($conn);
              if ($number > 0){
              	return 'Gelieve je aanwezigheid op training aan te geven';
		}
       }

	function getNumberOfGamesNotEntered($lidid){
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		$stmt="select count(wed.datum) from weds wed ";
		$stmt.=" join ploe ploeg on (wed.ploid=ploeg.ploid and ploeg.catid =4)";
		$stmt.=" left join wede aanw on (aanw.wedid=wed.wedid and aanw.lidid=" . $lidid . ")";
		$stmt.=" where wed.datum > CURRENT_DATE - 1";
		$stmt.=" and wed.datum < CURRENT_DATE + 20";
		$stmt.=" and aanw.SWAFW is null";
              $query = ibase_query($conn, $stmt);
	       while ($row = ibase_fetch_object($query)){
		   $number =$row->COUNT;
	       }
	       ibase_free_result($query);
              ibase_close($conn);
              if ($number > 0){
              	return 'Gelieve je aanwezigheid op wedstrijden aan te geven';
		}
       }



       function getTodoMessages($lidid){
              
		$arr[] = array ('msg'=>getLidgeldDatum($lidid));
              $training=getNumberOfTrainingsNotEntered($lidid);
              if ($training !=null){
			$arr[] = array ('msg'=>$training);
		}
		$games=getNumberOfGamesNotEntered($lidid);
              if ($games !=null){
			$arr[] = array ('msg'=>$games);
		}
		return $arr;
       }

	function sevensEntry(){
		 $conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
		 $stmt = "insert into tord (torid,pnaam,presp,email,adres,woonp,land,telnr,faxnr,wsite,categ)";
		 $stmt .=" values(2,'"; 
		 $stmt .= utf8_decode($_POST["rugbyclub"]) . "','";
		 $stmt .= utf8_decode($_POST["responsable"]) . "','";
		 $stmt .= utf8_decode($_POST["email"]) . "','";
		 $stmt .= utf8_decode($_POST["address"]) . "','";
		 $stmt .= utf8_decode($_POST["address2"]) . "','";
		 $stmt .= utf8_decode($_POST["country"]) . "','";
		 $stmt .= utf8_decode($_POST["tel"]) . "','";
		 $stmt .= utf8_decode($_POST["fax"]) . "','";
		 $stmt .= utf8_decode($_POST["website"]) . "','";
		 $stmt .= utf8_decode($_POST["category"]) . "')";
               $query = ibase_query($conn, $stmt);
               

		 $errors = array();
		 if(sizeof($errors)) {
				return  array("success"=>false, "errors"=>$errors);
		 } else {
				$to = "sevens@rugbyclubleuven.be";
				$headers = "From: RugbyClubLeuven" . "X-Mailer: php";
				$subject = "Sevens inschrijving!";
				$body = $_POST["rugbyclub"] . "(" . $_POST["email"] . ") heeft zich ingeschreven voor het sevens tornooi";
				mail($to, $subject, $body,$headers);
				return array("success"=>true);
		 }
	}

	function confirmAllWedPresence($lidid, $date){
		
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 

        	// vind alle overeenkomstige wedstrijden met zelfde date(WEDS)
		$stmt = "SELECT wedid FROM weds WHERE datum='" . $date . "'";
		$query = ibase_query($conn, $stmt);

              // www.firebirdfaq.org/faq220/
		// www.firebirdfaq.org/faq375/
        	while ($row = ibase_fetch_object($query)){
		   	$my_wedid =$row->WEDID;
                 	$stmt_in = "EXECUTE BLOCK AS BEGIN";
			$stmt_in .= " INSERT INTO wede(wedid,lidid,posit,swafw,swtra,swbeg,swspe)";
			$stmt_in .= " VALUES (" . $my_wedid . "," . $lidid . ",0,'F','F','F','T');";
			$stmt_in .= " WHEN SQLCODE -803 DO";
			$stmt_in .= " UPDATE wede SET swafw = 'F' WHERE lidid=" . $lidid . " and wedid = " . $my_wedid . ";";
			$stmt_in .= " END";

			$query2 = ibase_query($conn, $stmt_in);
			if($query2 != 1) {
				return array("success"=>false);
			}
	    	}

		return array("success"=>true);
	}

	function confirmAllWedAbsence($lidid, $date){
		
		$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 

        	// vind alle overeenkomstige wedstrijden met zelfde date(WEDS)
		$stmt = "SELECT wedid FROM weds WHERE datum='" . $date . "'";
		$query = ibase_query($conn, $stmt);

              // www.firebirdfaq.org/faq220/
		// www.firebirdfaq.org/faq375/
        	while ($row = ibase_fetch_object($query)){
		   	$my_wedid =$row->WEDID;
                 	$stmt_in = "EXECUTE BLOCK AS BEGIN";
			$stmt_in .= " INSERT INTO wede(wedid,lidid,posit,swafw,swtra,swbeg,swspe)";
			$stmt_in .= " VALUES (" . $my_wedid . "," . $lidid . ",0,'T','F','F','T');";
			$stmt_in .= " WHEN SQLCODE -803 DO";
			$stmt_in .= " UPDATE wede SET swafw = 'T' WHERE lidid=" . $lidid . " and wedid = " . $my_wedid . ";";
			$stmt_in .= " END";

			$query2 = ibase_query($conn, $stmt_in);
			if($query2 != 1) {
				return array("success"=>false);
			}
	    	}

		return array("success"=>true);
	}


	$method = $_POST["method"];
	$lidid = $_POST["lidid"];
	$wedid = $_POST["wedid"];
	$ploegid = $_POST["ploegid"];
	$insertorupdate = $_POST["ior"];
	$position = $_POST["position"];
	$date = $_POST["date"];
	$trmid = $_POST["trmid"];
	$catid = $_POST["catid"];
	$trainer = $_POST["trainer"];
	
	if ($method == 'confirmAllWedPresence') {
		echo json_encode(confirmAllWedPresence($lidid, $date));
	}

	if ($method == 'confirmAllWedAbsence') {
		echo json_encode(confirmAllWedAbsence($lidid,$date));
	}
	
	if ($method =='getNextGame'){
		echo getNextGame($ploegid);
	}

	if ($method =='getWedstrijdDates'){
		echo json_encode(getWedstrijdDates($ploegid));
	}
	if ($method =='getSeniorWedstrijdDates'){
		   echo json_encode(getWedstrijdDates('4,7'));
	}


	if ($method =='getTrainingDates'){
           echo json_encode(getTrainingDates($catid));
	}

	if ($method =='getOpstelling'){
	   echo json_encode(getOpstelling($wedid));
	}

	if ($method =='getAllPlayers'){
	   echo json_encode(getAllPlayers());
	}
	if ($method =='getSpelerStatus'){
	  echo json_encode(getSpelerStatus($lidid));
     	}
	if ($method =='getPresentPlayers'){
	   echo json_encode(getPresentPlayers($date,'T',$catid));
	}
	if ($method =='getAbsentPlayers'){
	   echo json_encode(getPresentPlayers($date,'F',$catid));
	}
	if ($method =='getWedstrijdPlayers'){
	   echo json_encode(getWedstrijdPlayers($wedid,$_POST["absent"]));
	}

	if ($method =='getAanwWedstrijdDates'){
		echo json_encode(getAanwWedstrijdDates($lidid,$catid));
	}
	if ($method =='getGameResults'){
		echo json_encode(getGameResults());
	}
	if ($method =='confirmWedPresence'){
		echo confirmWedPresence($wedid,$lidid,$insertorupdate);
	}
	if ($method =='confirmWedAbsence'){
		echo confirmWedAbsence($wedid,$lidid,$insertorupdate);
	}
	if ($method =='updatePosition'){
		echo updatePosition($position,$lidid,$wedid);
	}
	if ($method =='sevensEntry'){
		echo json_encode(sevensEntry());
	}
	if ($method =='getAanwTrainingDates'){
		echo json_encode(getAanwTrainingDates($lidid,$catid));
	}
	if ($method =='confirmTrainingPresence'){
		echo confirmTrainingPresence($trmid,$lidid,$date,$insertorupdate);
	}
	if ($method =='confirmTrainingAbsence'){
		echo confirmTrainingAbsence($trmid,$lidid,$date,$insertorupdate);
	}
	if ($method =='confirmTrainingPresencePlayers'){
		echo confirmTrainingPresencePlayers($trmid,json_decode($_POST["playerIds"]),$date);
	}
       if ($method =='getTodoMessages'){
		echo json_encode(getTodoMessages($lidid));
	}
	if ($method =='getTrainersDates'){
		echo json_encode(getTrainersDates($lidid,$_POST["soort"]));
	}
       if ($method =='confirmTrainerTrainingPresence'){
		echo confirmTrainerTrainingPresence($trmid,$lidid,$date,$_POST["present"]);
	}
	if ($method =='confirmTrainerGamePresence'){
		echo confirmTrainerGamePresence($wedid,$lidid,$_POST["present"]);
	}
?>
